package edg.db;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class Parser {
	
	/**
	 * Parses the allocation file created by vsed listeners.
	 * @param fileLocation Location of the output text file.
	 * @param runId Run ID of simulation that created this file. 
	 */
	public static void parseAllocation(String fileLocation, int runId) {
		BufferedReader input = getBufferedReader(fileLocation);
		try {
			String line = null;
			
			int id = -1;
			int acuity = -1;
			int waitingRoom = -1;
			int insideED = -1;
			
			while((line = input.readLine()) != null) {
				// Hack
				if(line.length() == 0) {
					
				}
				// Meta data line
				else if(line.substring(0, 9).equals("# Patient")) {					
					int idIndex = line.indexOf("Patient No: ") + 12;
					String id_ = "";
					for(int i=idIndex; line.charAt(i) != ' '; i++) {
						id_ += line.charAt(i);
					}
					id = Integer.parseInt(id_);
					
					int acuityIndex = line.indexOf("TriageAcuity: ") + 14;
					String acuity_ = "";
					for(int i=acuityIndex; i<line.length(); i++) {
						acuity_ += line.charAt(i);
					}
					acuity = Integer.parseInt(acuity_);
				}
				// Length of stay data
				else if(line.substring(0, 11).equals("# LOS_Total")) {					
					int waitingRoomIndex = line.indexOf("LOS_WAITINGROOM: ") + 17;
					String waitingRoom_ = "";
					for(int i=waitingRoomIndex; line.charAt(i) != ' '; i++) {
						waitingRoom_ += line.charAt(i);
					}
					waitingRoom = Integer.parseInt(waitingRoom_);
					
					int insideEDIndex = line.indexOf("LOS_InsideED: ") + 14;
					String insideED_ = "";
					for(int i=insideEDIndex; i<line.length(); i++) {
						insideED_ += line.charAt(i);
					}
					insideED = Integer.parseInt(insideED_);
					
					Database.query(
							
							"INSERT INTO runs_patients (" +
								"run_id, " +
								"patient_id, " +
								"acuity, " +
								"waiting_room_los, " +
								"inside_ed_los" +
							") " +
							"VALUES(" +
								runId + ", " +
								id + ", " +
								acuity + ", " +
								waitingRoom + ", " +
								insideED +
							");"
							
						);
				}
			}
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	 * Parses the resource utilization file created by vsed listeners.
	 * @param fileLocation Location of the output text file.
	 * @param runId Run ID of simulation that created this file. 
	 */
	public static void parseResUtilization(String fileLocation, int runId) {
		BufferedReader input = getBufferedReader(fileLocation);
		try {
			String line = null;
			
			String resourceId = "";
			String type = "";
			double utilization = 0.0;
			
			while((line = input.readLine()) != null) {
				if(line.charAt(0) != '#') {
					int typeIndex = line.indexOf("-") + 1;
					String type_ = "";
					for(int i=typeIndex; line.charAt(i) != ':'; i++) {
						type_ += line.charAt(i);
					}
					
					resourceId = line.substring(0, typeIndex-1);
					
					if(type_.toLowerCase().contains("doctor")) {
						type = "doctor";
					}
					else if(type_.toLowerCase().contains("nurse")) {
						type = "nurse";
					}
					else if(type_.toLowerCase().contains("triage")) {
						type = "triage";
					}
					else if(type_.toLowerCase().contains("clerk")) {
						type = "clerk";
					}
					else if(type_.toLowerCase().contains("bed")) {
						type = "bed";
					}
					else {
						type = "UNKNOWN";
					}
					
					if(!type.equals("UNKNOWN")) {
						int tab = line.indexOf('\t') + 1;
						String utilization_ = "";
						for(int i=tab; line.charAt(i) != ' '; i++) {
							utilization_ += line.charAt(i);
						}
						utilization = Double.parseDouble(utilization_);
						
						Database.query(
								
								"INSERT INTO runs_resources (" +
									"run_id, " +
									"resource_id, " +
									"type, " +
									"utilization" +
								") " +
								"VALUES(" +
									runId + ", " +
									"'" + resourceId + "', " +
									"'" + type + "', " +
									utilization +
								");"
								
							);
					}
				}
			}

		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	 * Helper method for reading from files.
	 * @param fileLocation Location of the file to be read from.
	 * @return BufferedReader object for specified file.
	 */
	private static BufferedReader getBufferedReader(String fileLocation) {
		try {
			return new BufferedReader(new FileReader(new File(fileLocation)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
